package xx.ring;

import xx.ring.domain.Car;
import xx.ring.domain.Person;

import java.io.*;

public class MyUtil {
//    @SuppressWarnings("unchecked")
    public static <T extends Serializable> T clone(T obj) {
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(arrayOutputStream);
            oos.writeObject(obj);

            ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(arrayOutputStream.toByteArray());
            ObjectInputStream objectInputStream = new ObjectInputStream(arrayInputStream);
            return (T)objectInputStream.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person("王铮", "25", new Car("小轿车", "100"));
//        Person clone = clone(person);
        Person clone = person.clone();
        System.out.println(person);
        System.out.println(clone);
        System.out.println("--------------------------");


        person.setAge("26");
        person.getCar().setBrand("小跑车");
//        clone.setName("王迪");
//        clone.getCar().setMaxSpeed("200");
        System.out.println(person);
        System.out.println(clone);
    }
}
